package com.coupons.client;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONTokener;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class ClientTest {

	public static final String HTTP = "http://";
	public static final String IP = "localhost:8080";
	public static final String ROOT = "CouponsServer";

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String action = "registCustomerAction";
		String url = HTTP + IP + "/" + ROOT + "/" + action;
		System.out.println(url);
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost request = new HttpPost(url);

		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("name", "kaomj"));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
				"UTF-8");
		request.setEntity(entity);
		HttpResponse response = (HttpResponse) httpclient.execute(request);
		HttpEntity entityRes = response.getEntity();

		InputStream is = entityRes.getContent();
		byte[] bytes = new byte[256];
		StringBuffer sb = new StringBuffer();
		while (is.read(bytes) > 0) {
			sb.append(new String(bytes));
			bytes = new byte[256];
		}
		JSONObject resJO = JSONObject.fromObject(sb.toString());
		System.out.println(resJO.get("result"));
	}

}
